package com.ls.controller.linkman;

import com.ls.controller.linkman.model.AddLinkmanModel;
import com.ls.controller.linkman.model.BatchSetLabelOrTeamModel;
import com.ls.controller.linkman.model.EditLinkmanModel;
import com.ls.controller.linkman.model.QueryLinkmanModel;
import com.ls.service.ILinkmanService;
import com.ls.utils.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 联系人表，与customer关联 前端控制器
 * </p>
 * 需要加一个联系人表，用户和联系人是一对多的关系
 * @author pengyu
 * @since 2024-11-20
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/linkmanApp")
@Api(value = "app联系人", tags = "app联系人")
public class LinkmanAppController {
    private final ILinkmanService service;
    /** 联系人 */
    @GetMapping(value = "pageList")
    public ResultData pageList(QueryLinkmanModel model) {
        return service.pageList(model);
    }
    /**联系人数量*/
    @GetMapping(value = "getLinkmanCount")
    public ResultData getLinkmanCount(QueryLinkmanModel model) {
        return service.getLinkmanCount(model);
    }
    /** 查询单个联系人 */
    @GetMapping(value = "getLinkmanById")
    public ResultData getLinkmanById(Long linkmanId) {
        return service.getLinkmanById(linkmanId);
    }

    /** 新增联系人 */
    @PostMapping(value = "add")
    public ResultData add(@Valid @RequestBody AddLinkmanModel model) {
        return service.add(model);
    }

    /** 编辑联系人 */
    @PostMapping(value = "edit")
    public ResultData edit(@Valid @RequestBody EditLinkmanModel model) {
        return service.edit(model);
    }

    /** 删除联系人 */
    @PostMapping(value = "del")
    public ResultData del(@RequestParam Long linkmanId) {
        return service.del(linkmanId);
    }
    
    /** 批量删除联系人 */
    @PostMapping(value = "batchDel")
    public ResultData batchDel(@RequestBody List<Long> linkmanIds) {
        return service.batchDel(linkmanIds);
    }
    
    /**批量设置联系人的分组或标签*/
    @PostMapping(value = "batchSetLabelOrTeam")
    public ResultData batchSetLabelOrTeam(@RequestBody BatchSetLabelOrTeamModel model) {
        return service.batchSetLabelOrTeam(model);
    }
    /**excel导入联系人*/
    @PostMapping(value = "import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation("excel导入联系人")
    public ResultData importExcel(@RequestParam("file") MultipartFile file) {
        File tempFile = null;
        try {
            String tempFileName = System.getProperty("java.io.tmpdir")
                    + "linkman_import_" + UUID.randomUUID() + ".xlsx";
            tempFile = new File(tempFileName);

            // 直接写入目标文件（自动创建新文件）
            file.transferTo(tempFile);

            return service.importExcel(tempFile);
        } catch (IOException e) {
            return ResultData.failure("文件传输异常：" + e.getMessage());
        } catch (Exception e) {
            return ResultData.failure("系统处理异常：" + e.getMessage());
        } finally {
            // 强制删除临时文件（参考FileUtils实现）
            if (tempFile != null && tempFile.exists()) {
                tempFile.delete();
            }
        }
    }
    /**导入联系人数组*/
    @PostMapping(value = "importArray")
    @ApiOperation("导入联系人数组")
    public ResultData importArray(@RequestBody List<AddLinkmanModel> linkmanArray) {
        return service.importArray(linkmanArray);
    }
    /**判断联系人是否保存*/
    @PostMapping(value = "isSaved")
    public ResultData isSaved(@RequestBody List<String> phoneLists) {
        return service.isSaved(phoneLists);
    }
//    /**获取所有结构联系人*/
//    @GetMapping(value = "getAllLinkman")
//    public ResultData getAllLinkman() {
//        return service.getAllLinkman();
//    }
    
    /**查询组织架构*/
    @GetMapping(value = "getOrganizationStructure")
    @ApiOperation("查询组织架构")
    public ResultData getOrganizationStructure(QueryLinkmanModel model) {
        return service.getOrganizationStructure(model);
    }
}
